#!/bin/bash
# 彻底删除Windsurf（含缓存/配置/进程）- 修复变量未定义错误
# 使用：chmod +x uninstall_windsurf.sh && ./uninstall_windsurf.sh

# 关闭未定义变量报错的严格模式（避免特殊场景下的误报，同时手动控制变量初始化）
set -eo pipefail
clear

# ===================== 日志函数 =====================
info() { echo -e "\033[34m[INFO] $1\033[0m"; }
success() { echo -e "\033[32m[SUCCESS] $1\033[0m"; }
warn() { echo -e "\033[33m[WARN] $1\033[0m"; }
error() { echo -e "\033[31m[ERROR] $1\033[0m"; }

# ===================== 步骤1：强制终止所有相关进程 =====================
info "1. 终止Windsurf相关进程..."
# 安全获取进程ID（确保变量必初始化，避免未定义错误）
pids=$(pgrep -f "Windsurf|windsurf" 2>/dev/null || true)

if [ -n "$pids" ]; then
  info "发现进程ID：$pids，正在强制终止..."
  kill -9 $pids 2>/dev/null
  sleep 2  # 等待进程退出
  # 二次检查是否还有残留进程
  remaining_pids=$(pgrep -f "Windsurf|windsurf" 2>/dev/null || true)
  if [ -z "$remaining_pids" ]; then
    success "所有Windsurf进程已终止"
  else
    warn "仍有残留进程：$remaining_pids，请手动在活动监视器中结束"
  fi
else
  warn "未检测到运行中的Windsurf进程"
fi

# ===================== 步骤2：删除应用程序本体 =====================
info "\n2. 删除应用程序本体..."
app_paths=(
  "/Applications/Windsurf.app"
  "$HOME/Applications/Windsurf.app"  # 用户级安装路径
)

for path in "${app_paths[@]}"; do
  if [ -d "$path" ]; then
    info "删除应用：$path（需管理员权限）"
    sudo rm -rf "$path"
    [ -d "$path" ] && error "删除$path失败" || success "已删除$path"
  fi
done

# ===================== 步骤3：彻底清理缓存（核心） =====================
info "\n3. 清理所有缓存文件..."
cache_paths=(
  # 用户级缓存
  "$HOME/Library/Caches/com.codeium.windsurf"
  "$HOME/Library/Caches/Windsurf"
  "$HOME/.windsurf"
  # 系统级缓存
  "/Library/Caches/com.codeium.windsurf"
  # 临时缓存
  "/private/var/folders/*/*/*/com.codeium.windsurf"  # 系统临时缓存路径
  "/tmp/windsurf*"
)

for path in "${cache_paths[@]}"; do
  # 处理通配符路径（如/private/var/folders/*）
  if ls $path 2>/dev/null; then
    info "清理缓存：$path"
    sudo rm -rf $path 2>/dev/null
    success "已清理缓存：$path"
  else
    warn "无缓存文件：$path"
  fi
done

# ===================== 步骤4：删除配置/数据/日志 =====================
info "\n4. 删除配置与数据文件..."
data_paths=(
  # 核心配置目录
  "$HOME/.codeium/windsurf"
  # 偏好设置
  "$HOME/Library/Preferences/com.codeium.windsurf.plist"
  "$HOME/Library/Preferences/com.codeium.windsurf*.plist"  # 带版本号的配置
  # 应用支持数据
  "$HOME/Library/Application Support/Windsurf"
  "$HOME/Library/Application Support/com.codeium.windsurf"
  # 日志文件
  "$HOME/Library/Logs/Windsurf"
  "$HOME/Library/Logs/com.codeium.windsurf"
  # 系统级数据
  "/Library/Application Support/Codeium/Windsurf"
)

for path in "${data_paths[@]}"; do
  if [ -e "$path" ]; then
    info "删除数据：$path"
    sudo rm -rf "$path" 2>/dev/null
    [ -e "$path" ] && error "删除$path失败" || success "已删除$path"
  fi
done

# ===================== 步骤5：清理启动项与扩展 =====================
info "\n5. 移除启动项与系统扩展..."
launch_paths=(
  "/Library/LaunchAgents/com.codeium.windsurf.plist"
  "/Library/LaunchDaemons/com.codeium.windsurf.plist"
  "$HOME/Library/LaunchAgents/com.codeium.windsurf.plist"
  # 系统扩展（若有）
  "/Library/Extensions/Windsurf.kext"
)

for path in "${launch_paths[@]}"; do
  if [ -f "$path" ]; then
    info "移除启动项：$path"
    sudo rm -f "$path" 2>/dev/null
    [ -f "$path" ] && error "移除$path失败" || success "已移除$path"
  fi
done

# ===================== 步骤6：最终残留检查 =====================
info "\n6. 检查剩余残留文件（5秒）..."
# 搜索所有含windsurf的文件（排除系统核心目录）
remaining=$(sudo find / -name "*windsurf*" -o -name "*Windsurf*" 2>/dev/null | \
  grep -vE "/System/|/dev/|/sys/|/proc/|/Volumes/" | \
  grep -vE "\.DS_Store|/private/var/folders/.*\.plist$" || true)

if [ -n "$remaining" ]; then
  warn "发现以下可能残留（请手动确认是否删除）："
  echo -e "\033[37m$remaining\033[0m"
else
  success "未发现残留文件"
fi

# ===================== 完成提示 =====================
info "\n===== 卸载完成 ====="
success "Windsurf及其缓存、配置已彻底清理"
echo "建议操作："
echo "1. 清空废纸篓（右键→清倒废纸篓）"
echo "2. 重启电脑（确保所有残留进程退出）"
info "===================="